﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;
using System.Web.Http;

using Microsoft.Practices.Unity;

using ERPStore.Web.Routing;
using ERPStore.Extensions;
using ERPStore.NewsLetters.Models;

using Microsoft.AspNet.SignalR;

namespace ERPStore.Web.NewsLetters
{
	public class PluginInit : ERPStore.NewsLetters.PluginInit
	{
		public override void AddServices()
		{
			base.AddServices();

			Container.RegisterType<ERPStore.Services.IPersistentConnection, NewsLetterConnection>();
		}

		public override void Load()
		{
			base.Load();

			ViewEngines.Engines.Add(new NewsLetterViewEngine());

            var leadSourceRegistrationService = Container.Resolve<ERPStore.Services.ILeadSourceRegistrationService>();
			leadSourceRegistrationService.RegisterLeadSource(new ERPStore.NewsLetters.Models.NewsLetterSource());

			var serviceBus = Container.Resolve<ERPStore.Services.IServiceBus>();
			var medium = typeof(Ariane.MSMQMedium);
			if (ERPStore.GlobalConfiguration.Configuration.ExecutingContext == ERPStore.Models.ExecutingContext.Test)
			{
				medium = typeof(Ariane.InMemoryMedium);
			}

			serviceBus.RegisterQueue(ERPStore.NewsLetters.Queues.QUEUE_TRACK, null, medium);
			serviceBus.RegisterQueue(ERPStore.NewsLetters.Queues.QUEUE_BODY_CREATION, typeof(MessageReaders.NewsLetterBodyCreationRequestMessageReader), medium);
			serviceBus.RegisterQueue(ERPStore.NewsLetters.Queues.QUEUE_SCHEDULE_CAMPAIGN, null, medium);
			serviceBus.RegisterQueue(ERPStore.NewsLetters.Queues.QUEUE_SEND, null, medium);

			// GlobalHost.DependencyResolver = new IOC.SignalRUnityDependencyResolver(Container);
		}

		public override void RegisterRoutes()
		{
			var namespaces = new string[] { "ERPStore.Web.NewsLetters.Controllers" };

            // RouteTable.Routes.IgnoreRoute("newsletters/images/{*media}");

			RouteTable.Routes.MapERPStoreRoute(
				"NewsLetters.Unsubscribe"
				, "newsletters/unsubscribe/{*key}"
				, new { controller = "NewsLetters", action = "Unsubscribe", key = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"NewsLetters.Subscribe"
				, "newsletters/subscribe"
				, new { controller = "NewsLetters", action = "Subscribe" }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"NewsLetters.LandingPage"
				, "newsletters/lp/{landingLink}/{*key}"
				, new { controller = "NewsLetters", action = "Landing", landingLink = string.Empty, key = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"HtmlNewsLetterPreview"
				, "newsletterpreview/{*key}"
				, new { controller = "NewsLetters", action = "NewsLetterPreview", key = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"NewsLetterImages"
				, "newsletters/images/{*resource}"
				, new { controller = "NewsLetters", action = "Images", ressource = string.Empty }
				, namespaces
			);

            RouteTable.Routes.MapERPStoreRoute(
                "NewsLettersInternalRedirect"
				, "newsletters/ir/{route}/{*key}"
                , new { controller = "NewsLetters", action = "NLRedirect", key = string.Empty, route = string.Empty }
                , namespaces
            );

			RouteTable.Routes.MapERPStoreRoute(
				"NewsLetterProduct"
				, "newsletters/product/{productCode}/{*key}"
				, new { controller = "NewsLetters", action = "Product", productCode = string.Empty, key = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"NewsLetterProductCategory"
				, "newsletters/productcategory/{productCategoryCode}/{*key}"
				, new { controller = "NewsLetters", action = "ProductCategory", productCategoryCode = string.Empty, key = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"NewsLetterBrand"
				, "newsletters/brand/{brandName}/{*key}"
				, new { controller = "NewsLetters", action = "Brand", productCategoryCode = string.Empty, key = string.Empty }
				, namespaces
			);


			RouteTable.Routes.MapERPStoreRoute(
				"NewsLetterTag"
				, "newsletters/tag/{*key}"
				, new { controller = "NewsLetters", action = "Tag", key = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"NewsLettersRedirect"
				, "newsletters/rd/{*landing}"
				, new { controller = "NewsLetters", action = "Goto", landingLink = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"HtmlNewsLetter"
				, "newsletters/{*key}"
				, new { controller = "NewsLetters", action = "NewsLetter", key = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"AdminNewsLettersReport"
				, "admin/newsletters/report/{*campaignKey}"
				, new { controller = "AdminNewsLetters", action = "Report", campaignKey = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapERPStoreRoute(
				"NewsLetters.Admin"
				, "admin/newsletters/{action}/{id}"
				, new { controller = "AdminNewsLetters", action = "Admin", id = string.Empty }
				, namespaces
			);

			RouteTable.Routes.MapHttpRoute(
				"NewsLettersApi"
				, "api/newsletters/{action}/{*id}"
				, new { controller = "NewsLettersApi", action = "Ping" , id = UrlParameter.Optional }
				);
		}
	}
}
